/*-------------------------放大镜--------------------------*/

$(function() {

	$("#shangpin #PImgBox #simg").mouseenter(function(e) {

		$("#shangpin #PImgBox #simg #fang").css({
			"display": "block"
		});
		$("#shangpin #PImgBox #bimg").css({
			"display": "block"
		})
		$(document).mousemove(function(e) {
			var parl = $("#shangpin #PImgBox #simg").offset().left;
			var part = $("#shangpin #PImgBox #simg").offset().top;			
			var fdleft = e.pageX-parl-$("#shangpin #PImgBox #simg #fang").width()/2;
			var fdtop = e.pageY-part-$("#shangpin #PImgBox #simg #fang").height()/2;		
			var minX = 0;
			var maxX = $("#shangpin #PImgBox #simg").innerWidth() - $("#shangpin #PImgBox #simg #fang").outerWidth();
			var minY = 0;
			var maxY = $("#shangpin #PImgBox #simg").innerHeight() - $("#shangpin #PImgBox #simg #fang").outerHeight();
			if(fdleft <= minX) {
				fdleft = minX;
			} else if(fdleft >= maxX) {
				fdleft = maxX;
			}
			if(fdtop <= minY) {
				fdtop = minY;
			} else if(fdtop >= maxY) {
				fdtop = maxY;
			}

			$("#shangpin #PImgBox #simg #fang").css({
				"left": fdleft,
				"top": fdtop
			})
			var biliX = $("#shangpin #PImgBox #bimg img").innerWidth()/$("#shangpin #PImgBox #simg img").innerWidth()
			var biliY = $("#shangpin #PImgBox #bimg img").innerHeight()/$("#shangpin #PImgBox #simg img").innerHeight()
			var imgleft = -fdleft*biliX;
			var imgtop = -fdtop*biliY;

			$("#shangpin #PImgBox #bimg img").css({
				"left": imgleft,
				"top": imgtop
			})

		})

	})
	$("#shangpin #PImgBox #simg").mouseleave(function() {

		$("#shangpin #PImgBox #bimg").css({
			"display": "none"
		});
		$("#shangpin #PImgBox #simg #fang").css({
			"display": "none"
		});

	})

})




/*-------------------------购物车加减--------------------------*/
$(function(){
	$("#PInfo .p3 .input1").click(function(){
		var num =parseInt($("#PInfo .p3 .input2").val());
		num = num-1;
		if (num<=1) {
			num = 1;
		}
		$("#PInfo .p3 .input2").val(num);
	})
	$("#PInfo .p3 .input3").click(function(){
		var num =parseInt($("#PInfo .p3 .input2").val());
		num = num+1;
		$("#PInfo .p3 .input2").val(num);
	})
	$("#PInfo .p3 .input2").blur(function(){
		var num =$("#PInfo .p3 .input2").val();
		var re = /^[\d\s]+$/;
		if (re.test(num)) {
			$("#PInfo .p3 .input2").val(num);
		}else{
			$("#PInfo .p3 .input2").val("1");			
		}
	})		
})


//---------------------------------取值---------------------------------------
$(function(){
	var order = [];//有数反映出来的是true 没有返回出来的是false
	$(".buy").click(function(){
		var price = $("#shangpin #PInfo .price #price").html();//商品价格
		//console.log(price)
		var name = $("#shangpin #PInfo h3 #name").html();//商品名
		//console.log(name)
		var num = $("#shangpin #PInfo .shuliang #num").val();//商品数量
		var sum = ($("#shangpin #PInfo .shuliang #num").val()*$("#shangpin #PInfo .price #price").html())//商品总价
		//console.log(num)
		var orderItem;
		orderItem = {};
		orderItem.id = 
		orderItem.name = encodeURIComponent(name);
		orderItem.price =price;
		orderItem.num =num;
		orderItem.sum =sum;
		
		//console.log(orderItem);
		
		if(getCookie('order')){
			var cookie = JSON.parse(getCookie('order'));
			cookie.push(orderItem);
			setCookie('order', JSON.stringify(cookie),3);
		}else{
			order.push(orderItem);
			setCookie('order', JSON.stringify(order),3);	
		}
	})
})


//---------------------------购物车是空的--------------------------------

$(function(){
	$(".buy").click(function(){
		$("#mainWrap #carbox .m-mycart").css({"display":"none"});
		$("#mainWrap #carbox .m-nocart").css({"display":"none"});
	})
})


































